package javashitang;

import vo.Person;
import vo.Student;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class lambda1 {

    static List<Student> list = Arrays.asList(new Student(1, "ff", "中国"),
            new Student(2, "bb", "美国"),
            new Student(3, "cc", "法国"),
            new Student(4, "dd", "日本"),
            new Student(5, "dd", "日本"));

    //stream的静态方法
    public static void test1(){
        Stream<Integer> limit = Stream.iterate(0, (x) -> x + 3).limit(4);
        limit.forEach(System.out::println);

        Stream<Double> limit1 = Stream.generate(Math::random).limit(3);
        limit1.forEach(System.out::println);
    }

    //顺序流转并行流
    public static void test2(){
        Optional<Student> first = list.stream().parallel().filter(x -> x.getId() > 7).findFirst();
        System.out.println(first.orElse(new Student()).getId());
    }

    public static void test3(){
        List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);

        // 遍历输出符合条件的元素
        list.stream().filter(x -> x > 6).forEach(System.out::println);
        // 匹配第一个
        Optional<Integer> findFirst = list.stream().filter(x -> x > 6).findFirst();
        // 匹配任意（适用于并行流）
        Optional<Integer> findAny = list.parallelStream().filter(x -> x > 6).findAny();
        // 是否包含符合特定条件的元素
        boolean anyMatch = list.stream().anyMatch(x -> x < 6);
        System.out.println("匹配第一个值：" + findFirst.get());
        System.out.println("匹配任意一个值：" + findAny.get());
        System.out.println("是否存在大于6的值：" + anyMatch);
    }

    //获取string集合中最长的元素
    public static void test4(){
        List<String> list = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd");
        Optional<String> max = list.stream().max(Comparator.comparing(String::length));
        System.out.println(max.get());
    }

    //获取Integer集合中最大值
    public static void test5(){
        List<Integer> list = Arrays.asList(7, 6, 9, 4, 11, 6);
        Optional<Integer> max = list.stream().max(Integer::compareTo);
        System.out.println(max.get());
    }

    //员工工资最高的
    public static void test6(){
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 8900, 23, "male", "New York"));
        personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
        personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
        personList.add(new Person("Anni", 8200, 24, "female", "New York"));
        personList.add(new Person("Owen", 9500, 25, "male", "New York"));
        personList.add(new Person("Alisa", 7900, 26, "female", "New York"));

        Optional<Person> max = personList.stream().max(Comparator.comparingInt(Person::getSalary));
        System.out.println("员工工资最大值：" + max.get().getSalary());
    }

    //英文字符串数组的元素全部改为大写。整数数组每个元素+3。
    public static void test7(){
        String[] strArr = { "abcd", "bcdd", "defde", "fTr" };
        List<String> strList = Arrays.stream(strArr).map(String::toUpperCase).collect(Collectors.toList());

        List<Integer> intList = Arrays.asList(1, 3, 5, 7, 9, 11);
        List<Integer> intListNew = intList.stream().map(x -> x + 3).collect(Collectors.toList());

        System.out.println("每个元素大写：" + strList);
        System.out.println("每个元素+3：" + intListNew);
    }

    //将两个字符数组合并成一个新的字符数组

    public static void test8(){
        List<String> list = Arrays.asList("m,k,l,a", "1,3,5,7");

        List<String> listNew = list.stream().flatMap(e -> Stream.of(e.split(","))).collect(Collectors.toList());

        System.out.println("之前的 "+list);
        System.out.println(listNew);
    }

    public static void main(String[] args) {
        test8();
    }
}
